package com.px.user.mapper;

import com.px.user.entity.ChargeMoney;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ChargeMoneyMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into charge_money (userId,money,status,channel,remark,channelNo,paymentType) values (#{userId},#{money},#{status},#{channel},#{remark},#{channelNo},#{paymentType})")
    void insert(ChargeMoney chargeMoney);

    @Update({"<script>",
            "update charge_money",
            " <set>",
            "  <if test='status != null'>status=#{status},</if>",
            "  <if test='channel != null'>channel=#{channel},</if>",
            " </set>",
            "where id=#{id}",
            "</script>"})
    void updateStatus(ChargeMoney chargeMoney);

    @Delete("delete from charge_money where id=#{id}")
    void delete(Long id);

    @Select("select * from charge_money where id=#{id}")
    ChargeMoney findById(Long id);

    @Select({"<script>",
            "select charge_money.*,user.nickname from charge_money left join user on charge_money.userId=user.id",
            "<where>",
            " <if test='userId != null'>and charge_money.userId = #{userId}</if>",
            " <if test='startTime != null'>and charge_money.createTime >= #{startTime}</if>",
            " <if test='endTime != null'>and charge_money.createTime <![CDATA[<=]]> #{endTime}</if>",
            " <if test='status != null'>and charge_money.status = #{status}</if>",
            " <if test='channel != null'>and charge_money.channel = #{channel}</if>",
            "</where>",
            "order by ${orderField} desc limit #{startIndex},#{limit}",
            "</script>"})
    @Results({@Result(property = "nickname", column = "nickname")})
    List<ChargeMoney> paging(ChargeMoney chargeMoney);

    @Select({"<script>",
            "select count(1) from charge_money",
            "<where>",
            "<if test='userId != null'>and userId = #{userId}</if>",
            "<if test='startTime != null'>and createTime >= #{startTime}</if>",
            "<if test='endTime != null'>and createTime <![CDATA[<=]]> #{endTime}</if>",
            "<if test='status != null'>and status = #{status}</if>",
            "<if test='channel != null'>and channel = #{channel}</if>",
            "</where>",
            "</script>"})
    Integer pagingCount(ChargeMoney chargeMoney);
}
